package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    /**
     * 员工登陆
     *
     * @param emp
     * @return
     */
    /**
     * 员工登陆
     *
     * @param emp
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp empData = empService.findByUserNameAndPassword(emp.getUsername(), emp.getPassword());
        if (empData != null) {
            //登陆成功、生成令牌
            Map<String, Object> map = new HashMap<>();
            map.put("id", empData.getId());
            map.put("username", empData.getUsername());
            map.put("name", empData.getName());
            String token = JwtUtils.generateJwt(map);

            return Result.success(token);
        }
        //登陆失败
        return Result.error("登陆失败");
    }
}